package com.usian.springbootmysql.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.springbootmysql.dao.LoginDao;
import com.usian.springbootmysql.dao.UserDao;
import com.usian.springbootmysql.pojo.ResultVo;
import com.usian.springbootmysql.pojo.TbUser;
import com.usian.springbootmysql.service.LoginService;
import com.usian.springbootmysql.utils.AilCodeUtils;
import org.omg.CORBA.TIMEOUT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginDao loginDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserDao userDao;

    @Override
    public ResultVo sendCode(String phone) throws Exception {
        Boolean isPhone = Validator.isMobile(phone);
        if (isPhone) {
            int count = loginDao.getUserCountByPhone(phone);
            if (count>0){
                AilCodeUtils ailCodeUtils = new AilCodeUtils();
                ailCodeUtils.sendLoginCode(phone,"123455");
                stringRedisTemplate.opsForValue().set(phone,"123455");
                stringRedisTemplate.expire(phone,1, TimeUnit.MINUTES);
                return new ResultVo(200,"短信发送成功",null);
            }else {
                return new ResultVo(303,"手机号码格式错误",null);
            }
        } else {
            return new ResultVo(303,"手机号码格式错误",null);
        }
    }

    public ResultVo userLogin(String phone,String code){
        Boolean isHavephone = StrUtil.isEmpty(phone);
        Boolean isHavecode = StringUtils.isEmpty(code);
        if (!isHavephone){
            if (!isHavecode){
//                int count = loginDao.getUserCountByPhone(phone);
                LambdaQueryWrapper<TbUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(TbUser::getPhone,phone);
                TbUser tbUser = userDao.selectOne(lambdaQueryWrapper);
                if (tbUser!=null){
                    String redisCOde = stringRedisTemplate.opsForValue().get(phone);
                    if (!StrUtil.isEmpty(redisCOde)){
                        if (redisCOde.equals(code)){
                            return new ResultVo(200,"success",tbUser);
                        }else {
                            return new ResultVo(303,"验证码输入错误",null);
                        }
                    }else {
                        return new ResultVo(303,"验证码已超期",null);
                    }
                }else {
                    return new ResultVo(303,"此手机号不存在或尚未激活",null);
                }
            }else {
                return new ResultVo(303,"验证码必填",null);
            }
        }else {
            return new ResultVo(303,"手机号码格式错误",null);
        }
    }
}
